﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Results_Comparer
{
    public class AlertsViewBuilder
    {
        // Fields
        private Dictionary<string, string[]> alerts = new Dictionary<string, string[]>();
        private Dictionary<RdfId, List<RdfAttribute>> flaggedDiffs = new Dictionary<RdfId, List<RdfAttribute>>();
        private TreeNode root = new TreeNode("Alerts", null);

        /// <summary>
        /// Initialize an alerts view builder
        /// </summary>
        /// <param name="JSPath"></param>
        public AlertsViewBuilder(string JSPath)
        {            
            this.LoadAlertsConfig(JSPath);
        }

        /// <summary>
        /// Add an rdfID to attribute linkage
        /// </summary>
        /// <param name="kvp"></param>
        public void Build(KeyValuePair<RdfId, List<RdfAttribute>> kvp)
        {
            RdfId key = kvp.Key;
            if (this.alerts.ContainsKey(key.rdfType))
            {
                List<RdfAttribute> list = kvp.Value;
                string[] list2 = this.alerts[key.rdfType];
                foreach (RdfAttribute attribute in list)
                    foreach (string str in list2)
                        if (attribute.attributeName.Equals(str))
                            this.buildTree(key.rdfType, str, kvp);
            }
        }

        /// <summary>
        /// Build a tree
        /// </summary>
        /// <param name="rdfType"></param>
        /// <param name="alertAttr"></param>
        /// <param name="kvp"></param>
        private void buildTree(string rdfType, string alertAttr, KeyValuePair<RdfId, List<RdfAttribute>> kvp)
        {
            TreeNode node4;
            TreeNode node5;
            List<TreeNode> childs = this.root.childs;
            bool flag = false;
            foreach (TreeNode node in childs)
            {
                if (node.name.Equals(rdfType))
                {
                    flag = true;
                    List<TreeNode> list2 = node.childs;
                    bool flag2 = false;
                    foreach (TreeNode node2 in list2)
                    {
                        if (node2.name.Equals(alertAttr))
                        {
                            TreeNode item = new TreeNode(kvp.Key.getPathName(), kvp.Key.GetRdfId());
                            node2.childs.Add(item);
                            flag2 = true;
                        }
                    }
                    if (!flag2)
                    {
                        node4 = new TreeNode(alertAttr, null);
                        node.childs.Add(node4);
                        node5 = new TreeNode(kvp.Key.getPathName(), kvp.Key.GetRdfId());
                        node4.childs.Add(node5);
                    }
                }
            }
            if (!flag)
            {
                TreeNode node6 = new TreeNode(rdfType, null);
                this.root.childs.Add(node6);
                node4 = new TreeNode(alertAttr, null);
                node6.childs.Add(node4);
                node5 = new TreeNode(kvp.Key.getPathName(), kvp.Key.GetRdfId());
                node4.childs.Add(node5);
            }
        }

        /// <summary>
        /// Load our alert configuration file
        /// </summary>
        /// <param name="AlertConfigFile"></param>
        private void LoadAlertsConfig(String AlertConfigFile)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(AlertConfigFile);
            foreach (XmlElement xElem in document.GetElementsByTagName("alert").OfType<XmlElement>())
            {
                List<string> Attributes = new List<string>();
                foreach (XmlElement xAttr in xElem.SelectNodes("attributes").OfType<XmlElement>())
                    Attributes.Add(xAttr.Attributes["name"].Value);
                alerts.Add(xElem.Attributes["type"].Value, Attributes.ToArray());
            }

        }

        /// <summary>
        /// Process a tree node
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public TreeNode Process(Dictionary<RdfId, List<RdfAttribute>> dict)
        {
            foreach (KeyValuePair<RdfId, List<RdfAttribute>> pair in dict)            
                this.Build(pair);            
            return this.root;
        }

    }
}
